package com.shopx.auth.service.impl;


import com.shopx.auth.bean.SysUserRole;
import com.shopx.auth.dao.db1.SysUserRoleDao;
import com.shopx.auth.model.SysUserRoleModel;
import com.shopx.auth.service.ISysUserRoleService;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author lituo
 * @className: SysConfigServiceImpl
 * @description: 用户角色服务
 * @date 2021/12/5 11:24 AM
 */
@Service
public class SysUserRoleServiceImpl extends BaseService implements ISysUserRoleService {

  @Autowired
  private SysUserRoleDao sysUserRoleDao;

  @Autowired
  private ISysSeqService sysSeqService;

  @Override
  public SysUserRoleModel selectByPrimaryKey(SysUserRole sysUserRole) throws ShopxException {
    if (sysUserRole == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    return sysUserRoleDao.selectByPrimaryKey(sysUserRole);
  }

  @Override
  public int insertSelective(SysUserRole sysUserRole) throws ShopxException {
    if (sysUserRole == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserRoleDao.insertSelective(sysUserRole);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int updateByPrimaryKeySelective(SysUserRole sysUserRole) throws ShopxException {
    if (sysUserRole == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserRoleDao.updateByPrimaryKeySelective(sysUserRole);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int deleteByPrimaryKey(SysUserRole sysUserRole) throws ShopxException {
    if (sysUserRole == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserRoleDao.deleteByPrimaryKey(sysUserRole);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  private int selectCount(SysUserRoleModel sysUserRole) throws ShopxException {
    return sysUserRoleDao.selectCount(sysUserRole);
  }

  private List<SysUserRoleModel> selectList(SysUserRoleModel sysUserRole) throws ShopxException {
    List<SysUserRoleModel> list = sysUserRoleDao.selectList(sysUserRole);
    return list;
  }

  @Override
  public PageDTO<SysUserRoleModel> getListPage(SysUserRoleModel sysUserRole) throws ShopxException {
    if (sysUserRole == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    PageDTO<SysUserRoleModel> pageDTO = new PageDTO<SysUserRoleModel>();
    pageDTO.setiTotalDisplayRecords(selectCount(sysUserRole));
    pageDTO.setData(selectList(sysUserRole));
    return pageDTO;
  }


  public String selectRoleByUserId(SysUserRoleModel sysUserRole) {
    sysUserRole.setCpId(super.getUserCpId());
    List<SysUserRoleModel> list = sysUserRoleDao.selectRoleByUserId(sysUserRole);
    String roleNames = "";
    for (SysUserRoleModel modeL : list) {
      roleNames += modeL.getRoleName() + ",";
    }
    if (!StringUtil.isEmpty(roleNames) && roleNames.length() > 0) {
      roleNames = roleNames.substring(0, roleNames.length() - 1);
    }
    return roleNames;
  }

}

